package com.wl.zlmanage.controller;

import com.wl.zlmanage.bean.ApplySignInfo;
import com.wl.zlmanage.bean.ApprovalInfo;
import com.wl.zlmanage.bean.BSType;
import com.wl.zlmanage.bean.ExpendInfo;
import com.wl.zlmanage.bean.SignTypeInfo;
import com.wl.zlmanage.bean.User;
import com.wl.zlmanage.bean.ZlInfo;
import com.wl.zlmanage.bean.ZlSignInfo;
import com.wl.zlmanage.constant.ActivityConstant;
import com.wl.zlmanage.mapper.ApplyMapper;
import com.wl.zlmanage.mapper.ResourceMapper;
import com.wl.zlmanage.mapper.SignCustomMapper;
import com.wl.zlmanage.mapper.UserMapper;
import com.wl.zlmanage.utils.TimeUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by wanglei on 2018/5/25.
 */
@Controller
public class ExpendApprovalController {

    public static Map<Integer, List<SignTypeInfo>> signTypeMap = new HashMap<>();

    private Logger logger = LoggerFactory.getLogger(ExpendApprovalController.class);

    @Autowired
    ResourceMapper resourceDao;

    @Autowired
    SignCustomMapper signCustomDao;

    @Autowired
    ApplyMapper applyDao;

    @Autowired
    UserMapper userDao;

    @RequestMapping("/getExpendApprovalList.do")
    @ResponseBody
    public Map<String, Object> getExpendApprovalList(HttpSession session, @RequestParam("page") int page,
        @RequestParam("limit") int limit, @RequestParam("search") String search) {
        Map<String, Object> resultMap = new HashMap<>();
        String username = (String) session.getAttribute("username");
        int startIndex = (page - 1) * limit;
        User user = userDao.getUserById(username);
        Integer signType = 1;
        if(ActivityConstant.deptList.contains(user.getUserDept())){
            signType = 2;
        }
        initChildDept(user);
        List<ApplySignInfo> reList = applyDao.getExpendApprovalList(startIndex, limit, search, user,signType);
        Integer count = applyDao.getExpendApprovalListCount(search, user,signType);
        resultMap.put("data", reList);
        resultMap.put("count", count);
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        return resultMap;
    }

    @RequestMapping("/getExpendApprovalListNew.do")
    @ResponseBody
    public Map<String, Object> getExpendApprovalListNew(HttpSession session, @RequestParam("page") int page,
        @RequestParam("limit") int limit, @RequestParam("search") String search) {
        Map<String, Object> resultMap = new HashMap<>();
        String username = (String) session.getAttribute("username");
        int startIndex = (page - 1) * limit;
        User user = userDao.getUserById(username);
        Integer signType = 1;
        if(ActivityConstant.deptList.contains(user.getUserDept())){
            signType = 2;
        }
        initChildDept(user);
        List<ApplySignInfo> reList = applyDao.getExpendApprovalListNew(startIndex, limit, search, user,signType);
        Integer count = applyDao.getExpendApprovalListNewCount(search, user,signType);
        resultMap.put("data", reList);
        resultMap.put("count", count);
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        return resultMap;
    }

    @RequestMapping("/askExpendApplyApproval.do")
    @ResponseBody
    public String askExpendApplyApproval(HttpSession session, @RequestParam("applySignId") int applySignId,
        @RequestParam("approvalState") int approvalState,
        @RequestParam("approvalResult") int approvalResult,
        @RequestParam("approvalAdvice") String approvalAdvice) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (approvalResult == 1) {
            ApplySignInfo applySignInfo = applyDao.getExpendApprovalById(applySignId);
            //跳过行政
            if (approvalState == 2) {
                if(!ActivityConstant.KD.equals(applySignInfo.getApplyType())){
                    approvalState = 6;
                } else {
                    approvalState = 8;
                }
            } else if (approvalState == 6 || approvalState == 8) {
                approvalState = 4;
            } else {
                approvalState += 1;
            }
            applyDao.askExpendApplyApproval(applySignId, approvalState);
        } else {
            applyDao.askExpendApplyApproval(applySignId, 0);
        }
        if(approvalState == 4){
            String applyTime = TimeUtils.getCurrentDateString(TimeUtils.DATE_MONTH_FORMAT_SQL);
            applyDao.updateExpendTime(applySignId,applyTime);
        }
        String approvalResultStr = ActivityConstant.APPROVAL_PASS;
        if (approvalResult == 0) {
            approvalResultStr = ActivityConstant.APPROVAL_BACK;
        }

        ApprovalInfo ai = new ApprovalInfo();
        ai.setApprovalResult(approvalResultStr);
        ai.setApprovalAdvice(approvalAdvice);
        ai.setApprovalUserId(userId);
        ai.setSignId(applySignId);
        ai.setApprovalTime(TimeUtils.getCurrentDateString(TimeUtils.DATE_TIME_FORMAT));

        applyDao.saveExpendApprovalInfo(ai);

        return "success";
    }

    @RequestMapping("/askExpendApplyApprovalNew.do")
    @ResponseBody
    public String askExpendApplyApprovalNew(HttpSession session, @RequestParam("applySignId") int applySignId,
        @RequestParam("approvalState") int approvalState,
        @RequestParam("approvalResult") int approvalResult,
        @RequestParam("approvalAdvice") String approvalAdvice) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (approvalResult == 1) {
            applyDao.askExpendApplyApprovalNew(applySignId, 3);
            String applyTime = TimeUtils.getCurrentDateString(TimeUtils.DATE_MONTH_FORMAT_SQL);
            applyDao.updateExpendTimeNew(applySignId,applyTime);
        } else {
            applyDao.askExpendApplyApprovalNew(applySignId, 0);
        }
        String approvalResultStr = ActivityConstant.APPROVAL_PASS;
        if (approvalResult == 0) {
            approvalResultStr = ActivityConstant.APPROVAL_BACK;
        }

        ApprovalInfo ai = new ApprovalInfo();
        ai.setApprovalResult(approvalResultStr);
        ai.setApprovalAdvice(approvalAdvice);
        ai.setApprovalUserId(userId);
        ai.setSignId(applySignId);
        ai.setApprovalTime(TimeUtils.getCurrentDateString(TimeUtils.DATE_TIME_FORMAT));

        applyDao.saveExpendApprovalInfoNew(ai);

        return "success";
    }

    @RequestMapping("/getExpendApprovalBySignId.do")
    @ResponseBody
    public List<ApprovalInfo> getExpendApprovalBySignId(@RequestParam("applySignId") int applySignId) {
        List<ApprovalInfo> reList = applyDao.getExpendApprovalBySignId(applySignId);
        return reList;
    }

    @RequestMapping("/getExpendApprovalBySignIdNew.do")
    @ResponseBody
    public List<ApprovalInfo> getExpendApprovalBySignIdNew(@RequestParam("applySignId") int applySignId) {
        List<ApprovalInfo> reList = applyDao.getExpendApprovalBySignIdNew(applySignId);
        return reList;
    }

    @RequestMapping("/editExpendMark.do")
    @ResponseBody
    public String editExpendMark(@RequestParam("applySignId") int applySignId,
        @RequestParam("editMark") String editMark,@RequestParam("editPrice") String editPrice,
        @RequestParam("key") int key) {
        if(key == 0){
            //签约客户报销修改备注
            applyDao.updateExpendMark(applySignId,editMark,editPrice);
        }else {
            //未签约客户报销修改备注
            applyDao.updateExpendMarkNew(applySignId,editMark,editPrice);
        }
        return "success";
    }

    public static void initChildDept(User user){
        if(user.getUserDept() == 99){
            user.setChildDeptList(ActivityConstant.rcDeptList);
        } else if(user.getUserDept() == 100){
            user.setChildDeptList(ActivityConstant.deptList);
        }else {
            user.setChildDeptList(new ArrayList<>());
        }
    }

}
